<?php

namespace app\api\controller;

use app\admin\model\Admin;
use app\api\model\Announcement as BZModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Request;

class Announcement extends Common
{
    /**
     * @param Request $request
     * @param BZModel $announcement
     * @return \think\response\Json
     */
    public function store(Request $request, BZModel $announcement)
    {
        $res = $announcement::create($request::param());
        if ($res) {
            return $this->returnJson(200, 'ok', $res);
        } else {
            return $this->returnJson(500, $res);
        }
    }

    /**
     * @param $id
     * @param BZModel $announcement
     * @return \think\response\Json
     */
    public function show($id, BZModel $announcement)
    {
        try {
            $res = $announcement->findOrFail($id);
            return $this->returnJson(200, 'ok', $res);
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param BZModel $announcement
     * @return \think\response\Json
     */
    public function update($id, Request $request, BZModel $announcement)
    {
        try {
            $res = $announcement->findOrFail($id)->save($request::param());
            if ($res) {
                return $this->returnJson(200, $res);
            } else {
                return $this->returnJson(500, $res);
            }
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param $id
     * @param BZModel $announcement
     * @return \think\response\Json
     */
    public function delete($id, BZModel $announcement)
    {
        try {
            $res = $announcement->findOrFail($id)->delete();
            if ($res) {
                return $this->returnJson(200, $res);
            } else {
                return $this->returnJson(500, $res);
            }
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (\Exception $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param Request $request
     * @param BZModel $announcement
     * @return \think\response\Json
     */
    public function index(Request $request, BZModel $announcement)
    {
        try {
            return $this->returnJson(200, 'ok', $announcement->page(
                $request::param('page', 1),
                $request::param('size', 10)
            )->paginate($request::param('size', 10))->each(function ($v) {
                $v['publisher_name'] = Admin::find($v['publisher'])['name'];
                $v['release_time'] = date('Y-m-d H:i:s', $v['release_time']);
                return $v;
            }));
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }
}
